!(function(window, document) {
    function imgVerify(options) {
        this.options = { // 默认options值    ps:可按顺序传入自定义的值
            id: "", //容器ID
            canvasId: "verifyCanvas", // canvasId
            width: "100", // 默认canvas宽度
            height: "30", // 默认canvas高度
            type: "blend", // //图形验证码默认类型blend:数字字母混合类型、number:纯数字、letter:纯字母
            code: "" // 验证码字符串
        }

        if (Object.prototype.toString.call(options) == "[object Object]") { //判断传入参数类型  （传入的是对象）
            for (var i in options) { //根据传入的参数，修改默认参数值
                this.options[i] = options[i];
            }
        } else { // 传入的是字符串类型
            this.options.id = options;
        }

        this._init();
        this.refresh();
    }

    imgVerify.prototype = {
            /**
             * 版本号
             */
            version: "1.0.0",
            /**
             * 初始化方法
             */
            _init: function() {
                var con = document.getElementById(this.options.id), // 获取容器元素
                    canvas = document.createElement('canvas'); // 创建canvas画布
                this.options.width = con.offsetWidth > 0 ? con.offsetWidth : '100'; // 设置插件容器的宽度
                this.options.height = con.offsetHeight > 0 ? con.offsetHeight : '30'; // 设置插件容器的高度
                // 设置canvas属性
                canvas.id = this.options.canvasId;
                canvas.width = this.options.width;
                canvas.height = this.options.height;
                canvas.style.cursor = 'pointer'; // 手势
                canvas.innerHTML = "您的浏览器版本不支持canvas";
                con.appendChild(canvas); // 将canvas塞入容器中
                // canvas 点击触发事件
                // _this = this;
                // canvas.onclick = function() {
                //     _this.refresh()
                // }
            },
            /**
             * 生成验证码图形
             */
            refresh: function() {
                var canvas = document.getElementById(this.options.canvasId);
                if (canvas.getContext) {
                    var ctx = canvas.getContext('2d');
                } else {
                    return;
                }

                ctx.textBaseline = "middle";
                ctx.fillStyle = randomColor(180, 240);
                ctx.fillRect(0, 0, this.options.width, this.options.height);

                /**
                 * 判断验证码类型
                 * ps: 选择场景使用验证码的类型
                 */
                // if (this.options.type == "blend") {
                //     var txtArr = this.options.numArr.concat(this.options.letterArr);
                // } else if (this.options.type == "number") {
                //     var txtArr = this.options.numArr;
                // } else {
                //     var txtArr = this.options.letterArr;
                // }

                /**
                 * 绘制验证码图形
                 * @len [number] 字符长度
                 */
                var len = this.options.code.length;
                for (var i = 0; i < len; i++) {
                    var _i = i;
                    ctx.font = randomNum(this.options.height / 2, this.options.height) + 'px SimHei'; //随机生成字体大小
                    ctx.fillStyle = randomColor(50, 160); //随机生成字体颜色		
                    ctx.shadowOffsetX = randomNum(-3, 3);
                    ctx.shadowOffsetY = randomNum(-3, 3);
                    ctx.shadowBlur = randomNum(-3, 3);
                    ctx.shadowColor = "rgba(0, 0, 0, 0.3)";
                    // 绘制code的起始位置
                    var x = this.options.width / 4 * i + 5;
                    var y = this.options.height / 2;
                    // 绘制code旋转角度
                    var deg = randomNum(-30, 30);
                    // 设置旋转角度和坐标原点
                    ctx.translate(x, y);
                    ctx.rotate(deg * Math.PI / 180);
                    ctx.fillText(this.options.code[_i], 0, 0);
                    // 恢复旋转角度和坐标原点
                    ctx.rotate(-deg * Math.PI / 180);
                    ctx.translate(-x, -y);
                }
                /**
                 * 绘制干扰线
                 */
                for (var i = 0; i < 4; i++) {
                    ctx.strokeStyle = randomColor(40, 180);
                    ctx.beginPath();
                    ctx.moveTo(randomNum(0, this.options.width), randomNum(0, this.options.height));
                    ctx.lineTo(randomNum(0, this.options.width), randomNum(0, this.options.height));
                    ctx.stroke();
                }
                /**
                 * 绘制干扰点
                 */
                for (var i = 0; i < this.options.width / 4; i++) {
                    ctx.fillStyle = randomColor(0, 255);
                    ctx.beginPath();
                    ctx.arc(randomNum(0, this.options.width), randomNum(0, this.options.height), 1, 0, 2 * Math.PI);
                    ctx.fill();
                }
            }
        }
        /**
         * 生成一个随机数
         * @param {*} min 
         * @param {*} max 
         */
    function randomNum(min, max) {
        return Math.floor(Math.random() * (max - min) + min);
    }
    /**
     * 生成一个随机色
     * @param {*} min 
     * @param {*} max 
     */
    function randomColor(min, max) {
        var r = randomNum(min, max);
        var g = randomNum(min, max);
        var b = randomNum(min, max);
        return "rgb(" + r + "," + g + "," + b + ")";
    }
    window.imgVerify = imgVerify;
})(window, document)